package com.heima.homeservicemanagementsystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.homeservicemanagementsystem.entity.ImageVo;
import com.heima.homeservicemanagementsystem.utils.DelUtils;
import com.heima.homeservicemanagementsystem.utils.FileUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.heima.homeservicemanagementsystem.module.PageParams;
import com.heima.homeservicemanagementsystem.module.Result;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.heima.homeservicemanagementsystem.entity.Carouseimage;
import com.heima.homeservicemanagementsystem.service.CarouseimageService;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.UUID;

import static cn.dev33.satoken.SaManager.log;

/**
 * ;(carouseimage)表控制层
 *
 * @author : http://www.chiner.pro
 * @date : 2025-3-26
 */
@Tag(name = "对象功能接口")
@RestController
@RequestMapping("/carouseimage")
public class CarouseimageController {
    @Autowired
    private Environment environment;
    @Resource
    private CarouseimageService carouseimageService;

    @Operation(summary = "分页查询")
    @PostMapping("/page")
    public Result<Page<Carouseimage>> paginQuery(@RequestBody PageParams<Carouseimage> pageParams) {
        //查询参数
        Carouseimage params = pageParams.getParams();
        LambdaQueryWrapper<Carouseimage> lqw = new LambdaQueryWrapper<>();

        lqw.eq(params.getUid() != null, Carouseimage::getUid, params.getUid());


        return Result.success(carouseimageService.page(pageParams.getPage(), lqw));
    }

    @Operation(summary = "查询图片")
    @PostMapping("/getimage")
    public Result getimage() {


        return Result.success(carouseimageService.list());
    }

    @PostMapping("/upload")
    @Operation(summary = "单文件上传")
    public Result<ArrayList<ImageVo>> upload(@RequestParam("file") MultipartFile file) {
        log.info("文件:{}", file);
        String port = environment.getProperty("server.port");
//        System.out.println("Server port: " + port);
        String upload = FileUtils.upload(file);
        ImageVo imageVo = new ImageVo();
        String randomId = UUID.randomUUID().toString();
        imageVo.setUid(randomId);
        imageVo.setName(upload);
        imageVo.setStatus("done");
        imageVo.setUrl("http://127.0.0.1:" + port + "/upload/" + upload);
        ArrayList<ImageVo> objects = new ArrayList<>();
        objects.add(imageVo);
        return Result.success("文件上传成功！", objects);


    }

    //    @Transactional(rollbackFor = Exception.class)  // 添加事务管理
    @PostMapping("/del")
    @Operation(summary = "单文件录播图删除")
    public Result<String> DelFile(@RequestBody Carouseimage filename) throws Exception {
        String filename1 = filename.getName();
        log.info("文件:{}", filename);

//        System.out.println("Server port: " + port);
        if (carouseimageService.removeBystrId(filename.getUid())) {
            boolean b = DelUtils.delFile(filename1);
            if (b) {
                return Result.success("文件删除成功！");
            } else {
                throw new Exception("文件删除失败");
            }


        }

        return Result.error("文件删除失败！");

    }

    /**
     * 新增数据
     *
     * @param carouseimage 实例对象
     * @return 实例对象
     */
    @Operation(summary = "新增数据")
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody Carouseimage carouseimage) {
        if (carouseimageService.save(carouseimage)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 更新数据
     *
     * @param carouseimage 实例对象
     * @return 实例对象
     */
    @Operation(summary = "更新数据")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody Carouseimage carouseimage) {
        if (carouseimageService.updateById(carouseimage)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 通过主键删除数据
     *
     * @param uid 主键
     * @return 是否成功
     */
    @Operation(summary = "通过主键删除数据",
            parameters = {
                    @Parameter(name = "uid", description = "主键", required = true, in = ParameterIn.PATH)
            })
    @GetMapping("/delete/{uid}")
    public Result<Boolean> deleteById(@PathVariable String uid) {

        if (carouseimageService.removeBystrId(uid)) {
            return Result.success();
        }
        return Result.error();
    }

}